package com.fengba.nanzhi.tabbar04.wz;

import android.app.Activity;
import android.content.Intent;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.fengba.nanzhi.fengba.R;
import com.fengba.nanzhi.utils.OKManager;
import com.fengba.nanzhi.utils.Preferences;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class PayMentActivity extends AppCompatActivity {
    private TextView PayMentNumber;
    private RecyclerView PayMentTab;
    private String level;
    private ImageButton PayMentfh;
    private String money;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.wz_activity_pay_ment);
        //获取上一个页面的值
        Intent intent = getIntent();
         money = intent.getStringExtra("Money");
        PayMentNumber = findViewById(R.id.PayMentNumber);
        PayMentNumber.setText("￥"+money+".00");


        init();
        //关闭页面
        PayMentfh = findViewById(R.id.PayMentfh);
        PayMentfh.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });

        final RefreshLayout refreshLayout2 = (RefreshLayout)findViewById(R.id.PayMentLayout);
        refreshLayout2.setOnRefreshListener(new OnRefreshListener() {
            public void onRefresh(RefreshLayout refreshlayout) {
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        init();
                        Toast.makeText(PayMentActivity.this, "查询成功", Toast.LENGTH_SHORT).show();
                        refreshLayout2.finishRefresh();

                        /**
                         * 刷新完成后调用此方法，要不然刷新效果不消失
                         */

                    }
                }, 0);
            }
        });
    }

    public void init(){
        Object user_info = Preferences.getParam(PayMentActivity.this,"user_info","");
        try {
            JSONObject myJson = new JSONObject(user_info.toString());
            //账号
            level = myJson.get("level_id").toString();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        OKManager.getInstance().type1011(level, "1", new OKManager.func4() {
            @Override
            public void onResponse(JSONObject jsonObject) {
                JSONArray jsonArray = null;
                try {
                    jsonArray = (JSONArray) jsonObject.get("data");
                    //加载列表
                    PayMentTab = findViewById(R.id.PayMentTab);
                    PayMentTab.setLayoutManager(new LinearLayoutManager(PayMentActivity.this));
                    PayMentTab.setAdapter(new PayMentAdapter(PayMentActivity.this,jsonArray,"￥"+money+".00",money));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(String string) {
                showToast(PayMentActivity.this,"网络错误");
            }
        });
    }
    public static void showToast(final Activity ctx, final String msg) {
        // 判断是在子线程，还是主线程
        if ("main".equals(Thread.currentThread().getName())) {
            Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
        } else {
            // 子线程
            ctx.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    Toast.makeText(ctx, msg, Toast.LENGTH_SHORT).show();
                }
            });
        }
    }
}
